from flask import views, render_template, request, url_for, \
    redirect, Blueprint, make_response
import json
from models import User
from mysql_config import db
from result import Result
from result_code import Code


register = Blueprint('register_control', __name__)


@register.route('/register', methods=['POST'])
def user_add():
    username = request.json.get("username")
    password = request.json.get("pwd")
    name = request.json.get("name")
    tel = request.json.get("tel")
    mail = request.json.get("mail")

    if username is None or password is None or name is None or tel is None:
        resp = vars(Result(Code.UPDATE_ERR.value, None, '注册失败'))
        return resp
    else:
        user = User(username=username, pwd=password, name=name, tel=tel,
                    mail=mail, actor=0)

        db.session.add(user)
        db.session.commit()
        resp = vars(Result(Code.UPDATE_OK.value, None, '注册成功'))
        return resp
